package com.kingdee.eas.hr.affair.client;
import com.kingdee.bos.Context;
import com.kingdee.bos.ctrl.extendcontrols.KDBizPromptBox;
import com.kingdee.bos.ctrl.kdf.table.*;
import com.kingdee.bos.ctrl.kdf.table.event.KDTEditEvent;
import com.kingdee.bos.ctrl.kdf.util.style.StyleAttributes;
import com.kingdee.bos.ctrl.swing.KDPromptSelector;
import com.kingdee.bos.dao.IObjectValue;
import com.kingdee.bos.dao.ormapping.ObjectUuidPK;
import com.kingdee.bos.metadata.entity.*;
import com.kingdee.bos.metadata.query.util.CompareType;
import com.kingdee.bos.util.BOSObjectType;
import com.kingdee.bos.util.BOSUuid;
import com.kingdee.eas.base.permission.*;
import com.kingdee.eas.basedata.org.*;
import com.kingdee.eas.basedata.org.client.PositionPromptBox2;
import com.kingdee.eas.basedata.person.PersonInfo;
import com.kingdee.eas.common.client.SysContext;
import com.kingdee.eas.fdc.basedata.FDCSQLBuilder;
import com.kingdee.eas.hr.affair.*;
import com.kingdee.eas.hr.base.batch.BatchProcessedResult;
import com.kingdee.eas.hr.base.client.IHRF7CommonRegister;
import com.kingdee.eas.hr.base.client.PlugInUIParam;
import com.kingdee.eas.hr.cifihr.BelongFactory;
import com.kingdee.eas.hr.cifihr.BelongInfo;
import com.kingdee.eas.hr.emp.*;
import com.kingdee.eas.util.SysUtil;
import com.kingdee.eas.util.client.*;
import com.kingdee.jdbc.rowset.IRowSet;

import java.util.*;

public class LoanBackBizBillEditUIBizCTEx extends LoanBackBizBillEditUIBiz {
	  protected IObjectValue createNewDetailData(PersonInfo pInfo)
	    {
	        LoanBackBizBillEntryInfo info = new LoanBackBizBillEntryInfo();
	        setLoanBackPerson(pInfo, info);
	        info.setPerson(pInfo);
	        PersonPositionInfo ppInfo = (PersonPositionInfo)pInfo.get("personPosition");
	        if(ppInfo != null)
	        {
	            info.setOldJobGrade(ppInfo.getJobGrade());
	            info.setOldJobLevel(ppInfo.getJobLevel());
	            info.setOldPosition(ppInfo.getPrimaryPosition());
	            info.setOldAdminOrg(ppInfo.getPersonDep());
	        }
	        return info;
	    }

	    private void setLoanBackPerson(PersonInfo pInfo, LoanBackBizBillEntryInfo loanEntryinfo)
	    {
	        try
	        {
	            if(IsPersonal)
	            {
	                StringBuffer oql = new StringBuffer();
	                oql.append("select *,outPosition.*,outAdmin.*,outJob.*,outJobLevel.*,outJobGrade.*,inPosition.*,inAdmin.*,inJob.*,inJobLevel.*,inJobGrade.* where state='");
	                oql.append("1").append("' and person.id ='").append(pInfo.getId().toString() + "'");
	                IPersonLoan ipersonLoan = PersonLoanFactory.getRemoteInstance();
	                PersonLoanCollection col = ipersonLoan.getPersonLoanCollection(oql.toString());
	                if(col.size() > 0)
	                {
	                    PersonLoanInfo personLoan = col.get(0);
	                    loanEntryinfo.setInitPosition(personLoan.getOutPosition());
	                    loanEntryinfo.setInitAdminOrg(personLoan.getOutAdmin());
	                    loanEntryinfo.setInitJob(personLoan.getOutJob());
	                    loanEntryinfo.setInitJobLevel(personLoan.getOutJobLevel());
	                    loanEntryinfo.setInitJobGrade(personLoan.getOutJobGrade());
	                }
	            }
	        }
	        catch(Exception e)
	        {
	            ExceptionHandler.handle(e);
	            SysUtil.abort();
	        }
	    }

	    public void addLine(boolean isInsert)
	        throws Exception
	    {
	        IsPersonal = false;
	        addLoanBackPerson(isInsert);
	    }

	    protected void addLoanBackPerson(boolean isInsert)
	        throws Exception
	    {
	        KDBizPromptBox f7 = new KDBizPromptBox();
	        f7.setQueryInfo("com.kingdee.eas.hr.affair.app.LoanBackPersonsQuery");
	        FilterInfo filter = new FilterInfo();
	        HROrgUnitInfo hro = (HROrgUnitInfo)ctx.getMainOrgContext().get(OrgType.HRO);
	        if(hro == null)
	        {
	            MsgBox.showError(EASResource.getString("com.kingdee.eas.hr.affair.AFFAIRAutoGenerateResource", "55_FlucInBizBillEditUIBiz"));
	            return;
	        }
	        filter.getFilterItems().add(new FilterItemInfo("hrOrg.id", hro.getId(), CompareType.EQUALS));
	        EntityViewInfo ev = new EntityViewInfo();
	        ev.setFilter(filter);
	        f7.setEntityViewInfo(ev);
	        f7.setEnabledMultiSelection(true);
	        f7.setDataBySelector();
	        if(!f7.getSelector().isCanceled())
	        {
	            List persons = new ArrayList();
	            String personLoanIds = "";
	            if(f7.getData() instanceof Object[])
	            {
	                Object objs[] = (Object[])f7.getData();
	                for(int i = 0; i < objs.length; i++)
	                {
	                    PersonLoanInfo personLoanInfo = (PersonLoanInfo)objs[i];
	                    persons.add(personLoanInfo.getPerson());
	                    personLoanIds = personLoanIds + "'" + personLoanInfo.getId() + "' ";
	                }

	            } else
	            {
	                PersonLoanInfo personLoanInfo = (PersonLoanInfo)f7.getData();
	                persons.add(personLoanInfo.getPerson());
	                personLoanIds = personLoanIds + "'" + personLoanInfo.getId() + "' ";
	            }
	            addLineByPersonList(persons, true);
	            personLoanIds = personLoanIds.trim().replaceAll(" ", ",");
	            afterAddPersonLoan(personLoanIds);
	        }
	    }

	    private void afterAddPersonLoan(String personLoanIds)
	        throws Exception
	    {
	        String oql = "select *,outPosition.*,outAdmin.*,outJob.*,outJobLevel.*,outJobGrade.*,inPosition.*,inAdmin.*,inJob.*,inJobLevel.*,inJobGrade.* where state='1' id in (" + personLoanIds + ")";
	        IPersonLoan ipersonLoan = PersonLoanFactory.getRemoteInstance();
	        PersonLoanCollection col = ipersonLoan.getPersonLoanCollection(oql);
	        Map personLoanMap = new HashMap();
	        PersonLoanInfo personLoan;
	        for(Iterator iterator = col.iterator(); iterator.hasNext(); personLoanMap.put(personLoan.getPerson().getId().toString(), personLoan))
	            personLoan = (PersonLoanInfo)iterator.next();

	        for(int i = 0; i < kdtEntrys.getRowCount(); i++)
	        {
	            IRow row = kdtEntrys.getRow(i);
	            PersonInfo person = (PersonInfo)row.getCell("person").getValue();
	            PersonLoanInfo personLoan1 = (PersonLoanInfo)personLoanMap.get(person.getId().toString());
	            if(personLoan1 != null)
	            {
	                row.getCell("initPosition").setValue(personLoan1.getOutPosition());
	                row.getCell("initAdminOrg").setValue(personLoan1.getOutAdmin());
	                row.getCell("initJob").setValue(personLoan1.getOutJob());
	                row.getCell("initJobLevel").setValue(personLoan1.getOutJobLevel());
	                row.getCell("initJobGrade").setValue(personLoan1.getOutJobGrade());
	                row.getCell("oldPosition").setValue(personLoan1.getInPosition());
	                row.getCell("oldAdminOrg").setValue(personLoan1.getInAdmin());
	                row.getCell("oldJob").setValue(personLoan1.getInJob());
	                row.getCell("oldJobLevel").setValue(personLoan1.getInJobLevel());
	                row.getCell("oldJobGrade").setValue(personLoan1.getInJobGrade());
	                row.getCell("position").setValue(personLoan1.getOutPosition());
	                row.getCell("adminOrg").setValue(personLoan1.getOutAdmin());
	                row.getCell("job").setValue(personLoan1.getOutJob());
	                row.getCell("jobLevel").setValue(personLoan1.getOutJobLevel());
	                row.getCell("jobGrade").setValue(personLoan1.getOutJobGrade());
	                FDCSQLBuilder sql = new FDCSQLBuilder();
	        		sql.appendSql("select  cfBelongID,fcontrolunitid from T_BD_Person where  fid='"+ person.getId().toString() + "'");
	        		String BlongID=null; 
	        		String unitID=null; 
	        		IRowSet	rowset = sql.executeQuery();
	        		while(rowset.next()){
	        			   BlongID = rowset.getString("cfBelongID");
	        			   break;
	        		}
	        		if(BlongID!=null){
	        			BelongInfo bInfo = BelongFactory.getRemoteInstance().getBelongInfo(new ObjectUuidPK(BlongID));
						row.getCell("oldBlongs").setValue(bInfo);
	        		}
	            }
	        }

	    }

	    protected boolean isAddPersonByF7()
	    {
	        return true;
	    }

	    protected String[] getNotDupColumn()
	    {
	        return (new String[] {
	            "person"
	        });
	    }

	    protected String[] getNotNullColumn()
	    {
	        return (new String[] {
	            "position", "adminOrg", "bizDate", "loanEndDate", "outPositionStr", "outAdminStr", "inPositionStr", "inAdminStr"
	        });
	    }

	    protected String getPrintQuery(String dsName)
	    {
	        getClass();
	        if("baseInfo".equals(dsName))
	            return "com.kingdee.eas.hr.affair.app.LoanBackBizBillPrintQuery";
	        getClass();
	        if("approveInfo".equals(dsName))
	            return "com.kingdee.eas.hr.affair.app.LoanBackBizBillApprovePrintQuery";
	        else
	            return null;
	    }

	    protected String getPrintPath()
	    {
	        return "/HR/AffairBiz/LoanBack";
	    }

	    public void initDataStatus()
	        throws Exception
	    {
	        super.initDataStatus();
	        UserInfo userInfo = SysContext.getSysContext().getCurrentUserInfo();
	        HROrgUnitInfo hrOrgInfo = SysContext.getSysContext().getCurrentHRUnit();
	        IPermission per = PermissionFactory.getRemoteInstance();
	        FieldPermission fields = per.getFieldPermission(BOSObjectType.create("E2673D70"), new ObjectUuidPK(userInfo.getId()), new ObjectUuidPK(hrOrgInfo.getId()));
	        IColumn col = kdtEntrys.getColumn(getJobLevelCol());
	        FieldAccessInfo accessInfo = fields.getFieldAccessInfo("entrys.jobLevel");
	        if(accessInfo != null && !accessInfo.isEditable() && col != null)
	            col.getStyleAttributes().setBackground(CANNOT_INPUT_COLOR);
	        col = kdtEntrys.getColumn(getJobGradeCol());
	        accessInfo = fields.getFieldAccessInfo("entrys.jobGrade");
	        if(accessInfo != null && !accessInfo.isEditable() && col != null)
	            col.getStyleAttributes().setBackground(CANNOT_INPUT_COLOR);
	    }

	    private boolean IsPersonal;

}
